<template>
  <view class="login-box">
    <view class="top-box">
      <view class="desc-box">
        <view class="desc-item" v-for="item in descList">{{item}}</view>
      </view>
    </view>
    <view class="logo-box">
      <image class="logo" src="/static/image/logo.png" mode="aspectFit"></image>
    </view>
    <view class="btn-box">
<!--      <button class="btn" type="primary" open-type="getPhoneNumber" @getphonenumber="getPhone">登录</button>-->
      <button class="btn" type="primary" @click="toNext">登录</button>
    </view>
  </view>
</template>

<script>
import { EDIT_PERMISSION_ROLES } from '@/constant/index.js'
export default {
  data() {
    return {
      qrGatewayId: '',
      hasUserName: false,
      descList: [
        "灯具免设置",
        "灯具自学习",
        "灯具自组网",
        "灯具全数据",
      ]
    }
  },
  onLoad() {
    const self = this
    const app = getApp()
    self.qrGatewayId = app.globalData.qrGatewayId
    uni.login({
      success (res) {
        if (res.code) {
          //发起网络请求
          uni.request({
            url: '/sys/login',
            method: 'POST',
            data: {
              code: res.code
            },
            success(result) {
              self.hasUserName = !!result.data.data?.name
              const app = getApp()
              console.log('qrGatewayId', app.globalData.qrGatewayId)
              console.log('qrVideoId', app.globalData.qrVideoId)
              self.toNext()
            }
          })
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
  },
  methods: {
    toNext() {
      const self = this
      self.toIndex()
      // if (!self.hasUserName) {
      //   self.toUserInfoSetting()
      // } else {
      //   self.toIndex()
      // }
    },
    toIndex() {
      uni.navigateTo({
        url: '/pages/entrance/index/index'
      })
    },
    toNoPermission() {
      uni.navigateTo({
        url: '/pages/entrance/no-permission/no-permission'
      })
    },
    toUserInfoSetting() {
      uni.navigateTo({
        url: '/pages/me/edit-info/edit-info?toIndex=1'
      })
    },
    getPhone (e) {
      if (!e.detail.code) {
        console.error(e.detail)
        uni.showToast({
          title: '登录失败',
          icon: 'none',
          duration: 2000
        })
        return
      }
      const self = this
      uni.request({
        url: '/sys/getPhoneNumber',
        method: 'POST',
        data: {
          code: e.detail.code
        },
        success(result) {
          if (result.data.success && result.data.data?.phoneNumber) {
            self.savePhoneNumber(result.data.data?.phoneNumber)
          }
        }
      })
    },
    savePhoneNumber(phoneNumber) {
      const self = this
      uni.request({
        url: '/user/modify',
        method: 'POST',
        data: {
          phone: phoneNumber
        },
        success(result) {
          if (result.data.success) {
            self.toNext()
          }
        }
      })
    },


  }
}
</script>

<style lang="scss" scoped>
.login-box {
  .top-box {
    background-color: $primary;
    height: 450rpx;
    .desc-box {
      display: flex;
      flex-wrap: wrap;
      justify-content: center;
      padding-top: 100rpx;
      .desc-item {
        padding: 20rpx;
        text-align: center;
        width: 300rpx;
        color: $white;
        font-size: $font-size-5;
      }
    }
  }
  .logo-box {
    margin-top: -100rpx;
    text-align: center;
    .logo {
      width: 200rpx;
      height: 200rpx;
      border-radius: 50%;
    }
  }
  .btn-box {
    margin-top: 80rpx;
    padding: 0 115rpx;
    .btn {
      text-align: center;
      background-color: $primary;
    }
  }
}
</style>
